package com.ksvip.next.components.util.serializer;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 类SerializeUtils.java的实现描述
 * 
 * @author liks 2018年9月13日 上午9:38:32
 */
public class SerializationUtils {
    private static Logger logger = LoggerFactory.getLogger(SerializationUtils.class);

    /**
     * 字符串反序列化为对象
     * 
     * @param serStr
     * @return
     * @throws UnsupportedEncodingException
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public  static Object getObjFromStr(final String serStr) {
        try {
            final String redStr = URLDecoder.decode(serStr, "UTF-8");
            final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(redStr.getBytes("ISO-8859-1"));

            final ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);

            final Object result = objectInputStream.readObject();
            objectInputStream.close();
            byteArrayInputStream.close();
            return result;
        } catch (final Exception e) {
            logger.info("", e);
        }
        return null;
    }

    /**
     * 对象序列化为字符串
     * 
     * @param obj
     * @return
     * @throws IOException
     * @throws UnsupportedEncodingException
     */
    public static String getStrFromObj(final Object obj) {
        try {
            final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            final ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(obj);
            String serStr = byteArrayOutputStream.toString("ISO-8859-1");
            serStr = URLEncoder.encode(serStr, "UTF-8");

            objectOutputStream.close();
            byteArrayOutputStream.close();
            return serStr;
        } catch (final Exception e) {
            logger.info("", e);
        }
        return null;
    }

    /**
     * 序列化
     * 
     * @param object
     * @return
     */
    public static byte[] serialize(final Object object) {
        ObjectOutputStream oos = null;
        ByteArrayOutputStream baos = null;
        try {
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(object);
            return baos.toByteArray();
        } catch (final Exception e) {
            logger.info("", e);
        }
        return null;
    }

    /**
     * 反序列化
     * 
     * @param bytes
     * @return
     */
    public static Object deserialize(final byte[] bytes) {
        ByteArrayInputStream bais = null;
        try {
            bais = new ByteArrayInputStream(bytes);
            final ObjectInputStream ois = new ObjectInputStream(bais);
            return ois.readObject();
        } catch (final Exception e) {
            logger.info("", e);
        }
        return null;
    }
}
